<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>eventEmitter</title>
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        img{max-width: 650px;}
    </style>
</head>
<body class="container">
<h1>eventEmitter</h1>
<h3>创建app.js，内容如下：</h3>
<div class="well">
    <xmp>
/**
* Created by ztt on 2017/11/13.
*/
/**
创建buffer：方式一
创建长度为 10 字节的 Buffer 实例：
**/
var buf=new Buffer(10);
console.log(buf,buf.toString());
console.log('值1：',buf);
/** 打印：值1： <Buffer 00 00 00 00 00 00 00 00 00 00> **/
console.log('值2：',buf.toString());
/** 打印：值2： **/

/**方式二：
通过给定的数组创建 Buffer 实例：
**/
var arrays=[10,20,30,40,50];
var buf2=new Buffer(arrays);
console.log(buf2);
/** 打印：<Buffer 0a 14 1e 28 32> **/

/**方式三
通过一个字符串来创建 Buffer 实例：
**/
var buf3 = new Buffer("www.runoob.com", "utf-8");
console.log(buf3);
/** 打印：<Buffer 77 77 77 2e 72 75 6e 6f 6f 62 2e 63 6f 6d> **/
console.log(buf3.toString());
/** 打印：www.runoob.com **/

/** todo:写入缓冲区
**/
var buf4=new Buffer(256);
var len=buf4.write("www.runoob.com");
console.log("写入字节数 : "+  len);
/** 打印：写入字节数 : 14 **/

/** todo:从缓冲区中读取
buf.toString([encoding[, start[, end]]])
**/
var buf5=new Buffer(26);
for(var i=0;i<26;i++){
buf5[i]=i+97;
}
console.log( buf5.toString('ascii'));
/** 打印：abcdefghijklmnopqrstuvwxyz **/
console.log(buf5.toString('ascii',1,5));
console.log(buf5.toString('utf-8',1,5));
console.log( buf5.toString(undefined,0,5));

/** todo:将buffer转成json
**/
var buf5 = new Buffer('www.runoob.com');
var json = buf5.toJSON(buf5);
console.log(json);
/**
打印：
{
type: 'Buffer',
data: [ 119, 119, 119, 46, 114, 117, 110, 111, 111, 98, 46, 99, 111, 109 ]
}
**/
/** todo:合并
Node 缓冲区合并的语法如下所示：
Buffer.concat(list[, totalLength])
**/
var buf6=new Buffer('Hello');
var buf7=new Buffer(' Word');
var buf8=Buffer.concat([buf6,buf7]);
console.log(buf8.toString());
var buf9=Buffer.concat([buf6,buf7],8);//打印：Hello Word
console.log(buf9.toString());//打印：Hello Wo

/** todo：缓冲区比较
buf.compare(otherBuffer);
**/
var buf10=new Buffer('a');
var buf11=new Buffer('b');
var result=buf10.compare(buf11);
console.log(result==0?'相等':(result<0?'a在b的前面':'a在b的后面'));

/** todo:缓冲区拷贝
* buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])
**/
var buffer12 = new Buffer('ABCDE');
// 拷贝一个缓冲区
var buffer13 = new Buffer(3);
buffer12.copy(buffer13);
console.log("buffer12 content: " + buffer12.toString());
console.log("buffer13 content: " + buffer13.toString());
for(var i=0;i<buffer12.length;i++){
var buffer14=new Buffer(buffer12.length);
buffer12.copy(buffer14,i);
console.log(i+"、buffer14 content: " + buffer14.toString());
}
/** 打印：
0、buffer14 content: ABCDE
1、buffer14 content:  ABCD
2、buffer14 content:   ABC
3、buffer14 content:    AB
4、buffer14 content:     A
* **/

/** todo:裁剪
buf.slice([start[, end]])
**/
var buffer15 = new Buffer('runoob');
// 剪切缓冲区
var buffer16 = buffer15.slice(0,2);
console.log("buffer16 content: " + buffer16.toString());
    </xmp>
</div>
<h3>在cmd中运行效果如下：</h3>
<div class="well">
    <img src="20171113215622.png"/>
</div>
</body>
</html>